<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"
    import="login.LoginMachine, java.util.ArrayList;"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Account Management</title>
</head>
<body>

<%
try {
	String lgnStatus = request.getParameter("status"); 
	if (lgnStatus.equals("failed")) {
		out.println("<div style='color:red'><p>An error occured when trying to create or update the account.</div>Please try again or create an account.</p>");
	} 
	if (lgnStatus.equals("updated"))
		out.println("<div style='color:red'><p>Your account information was updated.</p></div>");

		
}
catch(NullPointerException e) {
	//do nothing 
}
%>

<%
//checks for username and manager cookie. 
	
	String action = "Submit"; 
	Cookie[] cookies = request.getCookies();
	Cookie c = null; 
	ArrayList<String> userData = new ArrayList<String>(); 
	boolean cookieFound = false; 
	boolean isManager = false; 
	
	if (cookies.length > 0) {	
		for (int i=0; i < cookies.length; i++) {
			c = cookies[i]; 
			String name = c.getName(); 
			if (name.equals("isManager")) {
				String value = c.getValue(); 
				if (value.equals("true"))
					isManager = true; 
			}
		}
		for (int i = 0; i < cookies.length; i++) {
			c = cookies[i]; 
			String name = c.getName(); 
			if (name.equals("username")){
				cookieFound = true; 
				break; 
			}
		}
	}
	
	if (cookieFound == true) {
		LoginMachine lm = new LoginMachine(); 
		userData = lm.getUserData(c.getValue());
		action = "Update"; 
		out.println("<h3>Update your account information:</h3>"); 
	}
	else {
		for(int j=0; j<10; j++) {
			userData.add(""); 
		}
		out.println("<h3>Register a new account:</h3>"); 
	}

	
%> 


<form method=post action="NewAccount">
<table>
<tr><td>User name: </td><td><input type="text" id='username' name=username size=10 value='<%= userData.get(0) %>'></td></tr>
<tr><td>Password: </td><td><input type="password" name=password size=10 value='<%= userData.get(2) %>'></td></tr>
<tr><td>Email Address: </td><td><input type="text" name=email size=10 value='<%= userData.get(1) %>'></td></tr>
<tr><td>First Name: </td><td><input type="text" name=firstname size=10 value='<%= userData.get(3) %>'></td></tr>
<tr><td>Last Name: </td><td><input type="text" name=lastname size=10 value='<%= userData.get(4) %>'></td></tr>
<tr><td>Phone Number: </td><td><input type="text" name=contactnum size=10 value='<%= userData.get(5)%>'></td></tr>
<%
	
		out.println("<tr><td>Account Type: </td><td><select name=accounttype><option>Manager</option><option>Customer</option></select></td></tr>"); 	
%>
</table>
<%
	if (action.equals("Update")){
		out.println("<input type=\"hidden\" name=action value=\'Update\'>"); 
		if (isManager == true) {	 
			out.println("<p><input type=SUBMIT value=\'Update\'></p>");
		}
		else { 
			out.println("<p><input type=SUBMIT value=\'Update\'></p>");
		}
	}
	else{
		out.println("<input type=\"hidden\" name=action value=\'Register\'>"); 
		out.println("<p><input type=SUBMIT value=\'Register\'></p>"); 
		}
%>
</form>

</body>
</html>